package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.service.wages.IGzGenerateMonthlyPayrollService;
import com.gpframework.module.wages.domain.GzOfficeSalary;
import com.gpframework.module.wages.domain.GzPersonnelVersion;
import com.gpframework.module.wages.domain.GzTransfer;
import com.gpframework.module.wages.domain.GzWarehousePersonnel;
import com.gpframework.module.wages.domain.req.GzPersonnelVersionAdd;
import com.gpframework.module.wages.domain.req.GzPersonnelVersionReq;
import com.gpframework.module.wages.domain.req.GzPersonnelVersionUpdate;
import com.gpframework.module.wages.domain.vo.GzPersonnelVersionVo;
import com.gpframework.module.wages.service.IGzOfficeSalaryService;
import com.gpframework.module.wages.service.IGzPersonnelVersionService;
import com.gpframework.module.wages.service.IGzTransferService;
import com.gpframework.module.wages.service.IGzWarehousePersonnelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzPersonnelVersionController
 * @Author gpframework
 * @Date 2024-01-26
 * @Version 1.0.0
 * @Description 人事版本Controller
 */
@Validated
@Api(tags = "人事版本")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/personnelversion" )
public class GzPersonnelVersionController extends BaseController {

    private final IGzPersonnelVersionService iGzPersonnelVersionService;
    private final IGzGenerateMonthlyPayrollService iGzGenerateMonthlyPayrollService;
    private final IGzOfficeSalaryService iGzOfficeSalaryService;
    private final IGzTransferService iGzTransferService;
    private final IGzWarehousePersonnelService iGzWarehousePersonnelService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-26
  * @Param gzPersonnelVersion
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    //@PreAuthorize("@ss.hasPermi('wages:personnelversion:list')")
    @GetMapping("/list")
    public TableDataInfo<GzPersonnelVersionVo> list(GzPersonnelVersionReq gzPersonnelVersionReq)
    {
        startPage();
        QueryWrapper<GzPersonnelVersion> lqw = new QueryWrapper<GzPersonnelVersion>();
        gzPersonnelVersionReq.generatorQuery(lqw,true);
        List<GzPersonnelVersionVo> list = iGzPersonnelVersionService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-26
     * @Param gzPersonnelVersion
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:personnelversion:add')" )
    @Log(title = "添加人事版本" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzPersonnelVersionAdd gzPersonnelVersionAdd) {
        GzPersonnelVersion gzPersonnelVersion = Convert.convert(new TypeReference<GzPersonnelVersion>() {}, gzPersonnelVersionAdd);
        return toAjax(iGzPersonnelVersionService.save(gzPersonnelVersion) ? 1 : 0);
    }


    @ApiOperation("生成人事数据")
    //@PreAuthorize("@ss.hasPermi('wages:personnelversion:generatePersonnelVersion')")
    @PostMapping("/generatePersonnelVersion")
    public AjaxResult generatePersonnelVersion(@RequestBody GzPersonnelVersionReq gzPersonnelVersionReq)
    {
            //删除 某月人事版本全部数据
            iGzPersonnelVersionService.remove(new LambdaQueryWrapper<GzPersonnelVersion>().eq(GzPersonnelVersion::getMonth, gzPersonnelVersionReq.getMonth()));
            //查询当月办公室及档口
            List<GzOfficeSalary> gzOfficeSalariesList = iGzOfficeSalaryService.list(new LambdaQueryWrapper<GzOfficeSalary>().eq(GzOfficeSalary::getMonth, gzPersonnelVersionReq.getMonth()).eq(GzOfficeSalary::getDelStatus, YesNoEnum.NO.getKey()));
            ////生成当月办公室及档口人事数据
            iGzGenerateMonthlyPayrollService.generateSalaryBill(gzOfficeSalariesList);
            //查询当月中转
            List<GzTransfer> gzTransfersList = iGzTransferService.list(new LambdaQueryWrapper<GzTransfer>().eq(GzTransfer::getMonth, gzPersonnelVersionReq.getMonth()).eq(GzTransfer::getDelStatus, YesNoEnum.NO.getKey()));
            //生成当月中转人事数据
            iGzGenerateMonthlyPayrollService.generateSalaryBillMatter(gzTransfersList);
            //查询当月仓库
            List<GzWarehousePersonnel> warehousePersonnelList = iGzWarehousePersonnelService.list(new LambdaQueryWrapper<GzWarehousePersonnel>().eq(GzWarehousePersonnel::getMonth, gzPersonnelVersionReq.getMonth()).eq(GzWarehousePersonnel::getDelStatus, YesNoEnum.NO.getKey()));
            //生成当月仓库
            iGzGenerateMonthlyPayrollService.generateWarehouse(warehousePersonnelList);

        return  AjaxResult.success();
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2024-01-26
     * @Param gzPersonnelVersion
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('wages:personnelversion:edit')" )
    @Log(title = "修改人事版本" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzPersonnelVersionUpdate gzPersonnelVersionUpdate) {
        GzPersonnelVersion gzPersonnelVersion = Convert.convert(new TypeReference<GzPersonnelVersion>() {}, gzPersonnelVersionUpdate);
        return toAjax(iGzPersonnelVersionService.updateById(gzPersonnelVersion) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-26
     * @Param gzPersonnelVersion
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:personnelversion:export')" )
    @Log(title = "导出人事版本" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzPersonnelVersionReq gzPersonnelVersionReq) {
        QueryWrapper<GzPersonnelVersion> lqw = new QueryWrapper<GzPersonnelVersion>();
        gzPersonnelVersionReq.generatorQuery(lqw,true);
        List<GzPersonnelVersionVo> list = iGzPersonnelVersionService.listVo(lqw);
        ExcelUtil<GzPersonnelVersionVo> util = new ExcelUtil<GzPersonnelVersionVo>(GzPersonnelVersionVo. class);
        return util.exportExcel(list, "personnelversion" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-26
     * @Param gzPersonnelVersion
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:personnelversion:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzPersonnelVersionVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzPersonnelVersionService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-26
     * @Param gzPersonnelVersion
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:personnelversion:remove')" )
    @Log(title = "删除人事版本" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzPersonnelVersionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
